package com.ruoyi.bizsys.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.bizsys.domain.SmsTask;
import com.ruoyi.bizsys.service.ISmsTaskService;
import com.ruoyi.bizsys.utils.SmsTaskExcelUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bizsys.domain.SmsTemplate;
import com.ruoyi.bizsys.service.ISmsTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 短信模板Controller
 *
 * @author african-loan
 * @date 2021-12-30
 */
@RestController
@RequestMapping("/bizsys/sms_template")
public class SmsTemplateController extends BaseController {
    @Autowired
    private ISmsTemplateService smsTemplateService;

    @Autowired
    private ISmsTaskService smsTaskService;

    @Autowired
    private SmsTaskExcelUtil smsTaskExcelUtil;

    /**
     * 查询短信模板列表
     */
    @PreAuthorize("@ss.hasPermi('bizsys:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmsTemplate smsTemplate) {
        startPage();
        List<SmsTemplate> list = smsTemplateService.selectSmsTemplateList(smsTemplate);
        return getDataTable(list);
    }
    
    /**
     * 导出短信模板列表
     */
    @PreAuthorize("@ss.hasPermi('bizsys:template:export')")
    @Log(title = "短信模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmsTemplate smsTemplate) {
        List<SmsTemplate> list = smsTemplateService.selectSmsTemplateList(smsTemplate);
        ExcelUtil<SmsTemplate> util = new ExcelUtil<SmsTemplate>(SmsTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 获取短信模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('bizsys:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(smsTemplateService.selectSmsTemplateById(id));
    }

    /**
     * 新增短信模板
     */
    @PreAuthorize("@ss.hasPermi('bizsys:template:add')")
    @Log(title = "短信模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmsTemplate smsTemplate) {
        return toAjax(smsTemplateService.insertSmsTemplate(smsTemplate));
    }

    /**
     * 修改短信模板
     */
    @PreAuthorize("@ss.hasPermi('bizsys:template:edit')")
    @Log(title = "短信模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmsTemplate smsTemplate) {
        return toAjax(smsTemplateService.updateSmsTemplate(smsTemplate));
    }

    /**
     * 删除短信模板
     */
    @PreAuthorize("@ss.hasPermi('bizsys:template:remove')")
    @Log(title = "短信模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(smsTemplateService.deleteSmsTemplateByIds(ids));
    }

    /**
     * 查询模版
     *
     * @param smsTemplate
     * @return
     */
    @PostMapping("/smsTemplate")
    public AjaxResult smsTemplate(@RequestBody SmsTemplate smsTemplate) {
        //1.通过模版查询是否有记录
        List<SmsTemplate> smsTemplates = smsTemplateService.selectSmsTemplateList(smsTemplate);
        smsTemplate.setCreateTime(DateUtils.getNowDate());
        smsTemplate.setUpdateTime(DateUtils.getNowDate());
        //判断如果有则返回如果没有则添加
        if (!smsTemplates.isEmpty()) {
            return AjaxResult.success("该模版已存在");
        } else {
            //赠加模版
            int i = smsTemplateService.insertSmsTemplate(smsTemplate);
            if (i > 0) {
                return AjaxResult.success("新增成功");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 执行SQL脚本
     *
     * @param
     * @return
     */
    @PostMapping("/generateSmsTask")
    public AjaxResult generateSmsTask(@RequestBody String sql) {
        logger.info("sql:" + sql);
        String sqlStr = "INSERT INTO sms_task ( notice_type, sms_type, country_code, phone, content )("+sql+")";
        logger.info("sqlStr:"+sqlStr);
        int insert = smsTaskService.insertSql(sqlStr);
        if (insert > 0) {
            return AjaxResult.success("执行成功");
        }
        return AjaxResult.error("执行失败");
    }
    /**
     * 导入名单excel文件
     */
    @PostMapping("/import")
    public AjaxResult excelImport(MultipartFile file){
        List<SmsTask> smsTaskList = new ArrayList<>();
        try {
            smsTaskList = smsTaskExcelUtil.readExcel(smsTaskExcelUtil.M2F(file));
            logger.info("-------- crsUserList size :" + smsTaskList.size());
            smsTaskService.saveBatch(smsTaskList);
        } catch (Exception e) {
            logger.error("-----文件解析异常：",e);
        }
        return AjaxResult.success();
    }

}
